package printer;

import barecodes.Ean18Generator;
import com.appclient.DaoInstanceEnum;
import com.carpark.ejb.Model.ParkingpassesModel;
import com.wizardOther.OtherControlledScreen;
import com.wizardOther.OtherScreensController;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.control.Control;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;

/**
 *
 * @author Sawior
 */
public class PrinterController implements Initializable, OtherControlledScreen {

    private OtherScreensController myController;
    @FXML
    private Pane print;

    public void handleAction(ActionEvent event) {
        PrinterInstance.print(print);
    }

    private void addToGridPanePasses() {
        GridPane pane = createDefaultGridPaneSettings();
        ArrayList<ParkingpassesModel> passes = new ArrayList(DaoInstanceEnum.INSTANCE.getDao().getAllParkingPasses());
        int row = 0;
        int column = 0;
        for (ParkingpassesModel rows : passes) {
            Image barImage = Ean18Generator.saveEanFile(rows.getParkingPassId());
            ImageView barecode = new ImageView(barImage);
            pane.add(barecode, row, column);
            if (column < 6) {
                column++;
            } else {
                column = 0;
                row++;
            }
        }
        print.getChildren().add(pane);
    }

    private GridPane createDefaultGridPaneSettings() {
        GridPane grid = new GridPane();
        grid.setMinWidth(Control.USE_COMPUTED_SIZE);
        grid.setPrefWidth(Control.USE_COMPUTED_SIZE);
        grid.paddingProperty().setValue(new Insets(20));
        return grid;
    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {
    }

    @Override
    public void setScreenParent(OtherScreensController screenParent) {
        myController = screenParent;
        addToGridPanePasses();
    }
}
